package com.futures.questionbank.main;

import android.os.Bundle;

import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.futures.questionbank.R;
import com.futures.questionbank.fragment.HeqingFragment;
import com.futures.questionbank.fragment.HomeFragment;
import com.futures.questionbank.fragment.InformationFragment;
import com.futures.questionbank.fragment.MyFragment;


import java.util.ArrayList;

/**
 * author : huang
 * date   : 2020/6/15 0015 15:56
 * version: 1.0
 */
public class MainActivity2 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private ArrayList<Fragment> mFragments;
    public HomeFragment homeFragment = new HomeFragment();
    public InformationFragment informationFragment = new InformationFragment();
    public HeqingFragment heqingFragment = new HeqingFragment();
    public MyFragment myFragment = new MyFragment();

    private RadioGroup mRg_content_fragment;
    private RadioButton rb_main_my,rb_main_chat;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //  ButterKnife.bind(this);
        initFragments();
        initView();
    }

    private void initView() {
        mRg_content_fragment = (RadioGroup) findViewById(R.id.rg_content_fragment);
        rb_main_chat = findViewById(R.id.rb_main_chat);
        //  rb_main_chat =  findViewById(R.id.rb_main_chat);
        mRg_content_fragment.setOnCheckedChangeListener(this);
        //   changeFragment(0);
        replaceFragment("home");
    }



    private void initFragments() {
        mFragments = new ArrayList<>();
        mFragments.add(homeFragment);
        mFragments.add(informationFragment);
        mFragments.add(heqingFragment);
        mFragments.add(myFragment);

    }

    private void replaceFragment(String tag) {
        if (currentFragment != null) {
            Log.i("MainActivity", tag + "-----1");
            getSupportFragmentManager().beginTransaction().hide(currentFragment).commit();
        }
        currentFragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (currentFragment == null) {
            switch (tag) {
                case "home":
                    currentFragment = new HomeFragment();
                    break;
                case "circle":
                    currentFragment = new InformationFragment();
                    break;
                case "hang":
                    currentFragment = new HeqingFragment();
                    break;
                case "mine":
                    currentFragment = new MyFragment();
                    break;
                default:
            }
            Log.i("MainActivity", tag + "-----2");
            getSupportFragmentManager().beginTransaction().add(R.id.fl_container, currentFragment, tag).commit();
        } else {
            getSupportFragmentManager().beginTransaction().show(currentFragment).commit();
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int position = group.indexOfChild(group.findViewById(checkedId));
        // Fragment baseFragment = getFragment(position);
        //  switchFragment(mContent, baseFragment);
        switch (position){
            case 0:
                replaceFragment("home");
                break;
            case 1:
                replaceFragment("circle");
                break;
            case 2:
                replaceFragment("hang");
                break;
            case 3:
                replaceFragment("mine");
                break;
            default:
        }
    }
}


